import java.util.Scanner;

public class NGuessNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            generateAnswer();
            while (true) {
                getPlayerInput();
                compareGuessAnswer();
                if (isWin()) {
                    System.out.println("玩家胜利");
                    break;
                } else {
                    System.out.println("猜错了，再试一次");
                }
            }
                System.out.println("在玩一次(y/n)");
                String choice = scanner.next();
                if (!choice.equalsIgnoreCase("y")) {
                    break;
                }
            }
        }
    public static int[] a=new int[4];
    public static int[] guess=new int[4];
    public static int A=0;
    public static int[] generateAnswer() {
        for (int i = 0;i<4;) {
            boolean cf= true;
            int answer = (int) (Math.random() * 9);
            for (int j = 0; j <4; j++) {
                if (a[j] == answer) {
                    cf=false;
                    break;
                }
            }
            if(cf==true){
                a[i]=answer;
                i++;
            }
        }
        return a;
    }
    public static int[] getPlayerInput(){
        try {
            Scanner scan = new Scanner(System.in);
            for (int i = 0; i < 4;) {
                System.out.println("请输入大于0小于9的数字,这是第"+i+"个");
                int sc = scan.nextInt();
                if(sc>=0&&sc<=9){
                    guess[i]=sc;
                    i++;
                }
                else{
                    System.out.println("输入错误，请重新输入");
                }
            }
        }catch (Exception e){
            System.out.println("输入错误,请输入数字！");
        }
        return guess;
    }
    public static String compareGuessAnswer(){
         A=0;
        for(int i=0;i<4;i++){
            for (int j=0;j<4;j++){
                if(guess[j]==a[i]){
                    A++;
                    break;
                }
                }
            }
        System.out.println("一共猜对了"+A+"个");
        return "一共猜对了"+A+"个";
        }
    public static boolean isWin(){
        boolean dc = false;

        if(A==4){
            dc=true;
        }else {
            dc=false;
        }
        return dc;
    }
    }
